using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SkinningInformation;

namespace GameEngine
{
	public static class Render
	{
		public static void DrawBasicEffectModel(GameTime gameTime, Model model, ref Matrix view, ref Matrix projection, ref Matrix world, BoundingFrustum boundingFrustum)
		{
			foreach (ModelMesh mesh in model.Meshes)
			{
				if (boundingFrustum.Contains(mesh.BoundingSphere.Transform(world)) != ContainmentType.Disjoint)
				{
					foreach (BasicEffect effect in mesh.Effects)
					{
						effect.World = world;
						effect.View = view;
						effect.Projection = projection;
						effect.EnableDefaultLighting();
						effect.SpecularColor = Vector3.Zero;
					}
					mesh.Draw();
				}
			}
		}

		private static Matrix SkinnedModelBaseTransform = Matrix.CreateRotationX(MathHelper.PiOver2);
		public static void DrawSkinnedEffectModel(GameTime gameTime, Model model, AnimationPlayer animationPlayer, ref Matrix view, ref Matrix projection, ref Matrix world, BoundingFrustum boundingFrustum)
		{
			// TODO: Implement a BoundingSphere for the model to check culling upon it. It should contain all meshes in all posible animation states and if it is outside the frustum - cull the model without even updating the animation player...
			animationPlayer.Update(gameTime.ElapsedGameTime, true, Render.SkinnedModelBaseTransform * world);
			Matrix[] bones = animationPlayer.GetSkinTransforms();
			foreach(ModelMesh mesh in model.Meshes)
			{
				foreach(SkinnedEffect effect in mesh.Effects)
				{
					effect.SetBoneTransforms(bones);
					effect.View = view;
					effect.Projection = projection;
					effect.EnableDefaultLighting();
					effect.SpecularColor = Vector3.Zero;
				}
				if (boundingFrustum.Contains(mesh.BoundingSphere.Transform(world)) != ContainmentType.Disjoint)
				{
					mesh.Draw();
				}
			}
		}
	}
}
